﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Dry.General;

namespace Dry.Generators
{
    public class GeneratorBase : DryBase
    {
        protected static string _strNewLine = "\r\n";

        public static int CountLinesInString(string s)
        {
            int count = 1;
            int start = 0;
            while ((start = s.IndexOf('\n', start)) != -1)
            {
                count++;
                start++;
            }
            return count;
        }

        protected static Exception CreateFileFromString(string strRelativePath, string str)
        {
            try
            {
                StreamWriter sw = File.CreateText(Dry.ProjectPath + "\\" + strRelativePath);
                sw.Write(str);
                sw.Close();

                return null;
            }
            catch (Exception e)
            {
                return e ;
            }
        }

        protected static string GetHungarianFromDbType(string strDbType)
        {
            switch (strDbType)
            {
                case "int":
                    return "int";
                case "uniqueidentifier":
                    return "guid";
                case "datetime":
                    return "dte";
                case "bit":
                    return "bln";
                case "nvarchar":
                    return "str";
                case "char":
                    return "chr";
                case "binary":
                    return "byt";
                case "xml":
                    return "xml";
                case "ntext":
                    return "str";
                case "nchar":
                    return "str";
                case "bigint":
                    return "int";
                default:
                    throw new Exception("Can not handle the database type '" + strDbType + "'");
            }
        }

        protected static string GetCodeTypeFromDbType(string strDbType)
        {
            switch (strDbType)
            {
                case "int":
                    return "int";
                case "uniqueidentifier":
                    return "Guid";
                case "datetime":
                    return "DateTime";
                case "smalldatetime":
                    return "DateTime";
                case "bit":
                    return "bool";
                case "nvarchar":
                    return "string";
                case "char":
                    return "char";
                case "binary":
                    return "Byte[]";
                case "xml":
                    return "Xml";
                case "ntext":
                    return "string";
                case "nchar":
                    return "string";
                case "bigint":
                    return "Int64";
                case "float":
                    return "double";
                case "varbinary":
                    return "Byte[]";
                default:
                    throw new Exception("Can not handle the database type '" + strDbType + "'");
            }
        }

        protected static string GetFileHeader(string namespaceSetting, string mspractices5Setting, string generatedrylogSetting, string localizationSetting, string tableprefixSetting, string generateInnerJoinCodeSetting)
        {
            string strHeader = "";
            strHeader += "/* This file is generated by Dry Code Generator " + DateTime.Now + _strNewLine;
            strHeader += " * ===============================================================================" + _strNewLine;
            strHeader += " * Do not change anything in this file because it will be overridden the next time" + _strNewLine;
            strHeader += " * the code is generated. Instead create an partial class of this one in the" + _strNewLine;
            strHeader += " * custom directory above this one." + _strNewLine;
            strHeader += " * " + _strNewLine;
            strHeader += " * It was generated with the following settings:" + _strNewLine;

            if (!string.IsNullOrEmpty(namespaceSetting))
                strHeader += " * namespace = " + namespaceSetting + _strNewLine;

            if (!string.IsNullOrEmpty(mspractices5Setting))
                strHeader += " * mspractices5 = " + mspractices5Setting + _strNewLine;

            if (!string.IsNullOrEmpty(generatedrylogSetting))
                strHeader += " * generatedrylog = " + generatedrylogSetting + _strNewLine;

            if (!string.IsNullOrEmpty(localizationSetting))
                strHeader += " * localization = " + localizationSetting + _strNewLine;

            if (!string.IsNullOrEmpty(tableprefixSetting))
                strHeader += " * tableprefix = " + tableprefixSetting + _strNewLine;

            if (!string.IsNullOrEmpty(generateInnerJoinCodeSetting))
                strHeader += " * generateInnerJoinCode = " + generateInnerJoinCodeSetting + _strNewLine;

            strHeader += " */" + _strNewLine;

            return strHeader;
        }
    }
}
